export const base = {
    data(){
        return {
            modules:'users',
            data: [],
            filter:{
                total:0,
                page:1,
                limit:5,
                pagesize:[5,10,15,20],
                name:'',
                title:'',
                cateId:null
            },
            editId:0,
            name:'',
            title:'',
            ids:[],
            modalAdd:false,
            loading:true,
            inputData:null
        }
    },
    methods: {
        editData (id) {
        this.modalAdd=true;
        fetch(`http://localhost:3000/${this.modules}/data/${id}`,{
                method:'put',
                headers:(
                    {
            'Content-Type':'application/json',
            }
                )
            }).then(
                res=>{
                    return res.json();
                }
            ).then(
                data=>{
                    this.formValidate=data;
                    this.editId=id;
                    console,log(this.formValidate)
                }
            )
        },
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    if(this.editId.length>0){
                        fetch(`http://localhost:3000/${this.modules}/data/${this.editId}`,{
                        method:'put',
                        headers:{
                            'Content-Type':'application/json'
                        },
                        body:JSON.stringify(this.formValidate)
                    })
                    .then(res=>{
                        return res.json()
                        })
                    .then(data=>{
                        console.log(data);
                       this.getData()
                        })
                        Object.assign(this.$data.formValidate,this.$options.data().formValidate);
                        this.editId=0;
                        this.formValidate._id=null;               
                    }else{
                        this.formValidate.cateId=this.filter.cateId;
                       fetch(`http://localhost:3000/${this.modules}/data`,{
                        method:'post',
                        headers:{
                            'Content-Type':'application/json'
                        },
                        body:JSON.stringify(this.formValidate)
                    })
                    .then(res=>{
                        return res.json()
                        })
                    .then(data=>{
                        console.log(data)
                       this.getData();
                       console.log(this.formValidate)
                        })
                        Object.assign(this.$data.formValidate,this.$options.data().formValidate);
                    }           
                this.$Message.success('Success!');
                this.modalAdd=false;
                } else {
                    this.$Message.error('Fail!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },
        remove (id) {
            this.$Modal.confirm({
                title: '',
                content: '确定删除该条信息吗?',
                onOk:() => {
                    fetch(`http://localhost:3000/${this.modules}/data/${id}`,{
                        method:'delete',
                        headers:(
                            {
                    'Content-Type':'application/json',
                    }
                        )
                    }).then(
                        res=>{
                            return res.json();
                        }
                    ).then(
                        data=>{
        
                       this.$Message.info('数据已经删除成功');
                        this.getData();
                        }
                    )
                }
            });
        },
        removes(){
            this.$Modal.confirm({
                title: '',
                content: `已选择${this.ids.length}条,点击确认删除`,
                onOk:() => {
                    var len = this.ids.length;
                    for(let i=0;i<len;i++){
                        this.removeData(this.ids[i]);
                        this.getData();
                   
                    }
                    this.$Message.info('删除成功');
                  
                }
            });
        },
        removeData(id){
            fetch(`http://localhost:3000/${this.modules}/data/${id}`,{
                method:'delete',
                headers:{
                    'Content-Type':'application/json'
                }
            })
        },
        getData(){
            var bodyString = JSON.stringify(this.filter);
            fetch(`http://localhost:3000/${this.modules}/list`,{
                method:'post',
                headers:{
                    'Content-Type':'application/json'
                },
                body:bodyString
            })
            .then((res) => {
                return res.json();
            })
            .then((data) => {
                for(var i=0;i<data.rows.length;i++){
                    data.rows[i].comment = '';
                }
                this.filter.total = data.total;
                this.data = data.rows;
            });
        },
        onSelectionChangeHandler(selection){
            var  len = selection.length;
            for(let i=0;i<len;i++){
                if(this.ids.indexOf(selection[i]._id)===-1){
                    this.ids.push(selection[i]._id);
                } 
            }
            //console.log(selection)
        },
        onSelectCancelHandler(selection,row){
            this.ids.splice(this.ids.indexOf(row._id),1);
        },

        searchData(){
            this.getData();
        },
       
        onChangeHandler(page){
            this.filter.page = page;
            this.getData();
        },
        onPageSizeChangeHandler(limit){
            this.filter.limit = limit;
            this.getData();
        }
    },
    created(){
        this.getData();
    }
}